﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SpriterDotNetUnity;

public class Character : MonoBehaviour
{
	public CharacterState state;
	public UnityAnimator animator;
	int dmg;
	public MonsterModule monster;

	void Awake ()
	{

	}
	// Use this for initialization
	void Start ()
	{
		SpriterDotNetBehaviour aaa = GetComponent<SpriterDotNetBehaviour> ();
		animator = aaa.Animator;
		state = CharacterState.IDLE;
		animator.AnimationFinished += Animator_AnimationFinished;
		//var animations = animator.GetAnimations ();

	}

	public void Death ()
	{
		animator.Play ("death");
		// tinh toan tao enemy tiep theo
		StartCoroutine (DeathCoroutine ());
	}


	IEnumerator DeathCoroutine ()
	{
		yield return new WaitForSecondsRealtime (1F);
		GameplayControl.instance.NextEnemy ();
		Destroy (gameObject);
	}

	public void Attack (int _dmg, bool targetIsMonster = true)
	{
		//Debug.Log ("Damage la  : " + _dmg);
		animator.Play ("attack");
		if (targetIsMonster == true) {
			// tan cong monster
			StartCoroutine (TakeDmgMonster (_dmg));
		} else {
			// tan cong nguoi choi
			StartCoroutine (TakeDmgPlayer (_dmg));
		}
	}

	IEnumerator TakeDmgPlayer (int _dmg)
	{
		if (monster != null) {
			// lay audio_clip theo ten trong data cua monster
			AudioClipType ac = (AudioClipType)System.Enum.Parse (typeof(AudioClipType), monster.audioNam);
			AudioManager.instance.PlaySound (ac);
			//AudioManager.instance.PlayMusic(
		}
		// data la 0.5 giay het animation => den khoang giay thu 0.3 se co hieu ung
		yield return new WaitForSecondsRealtime (0.3F);
		if (GameplayControl.instance.playerShieldLevel > 0) {
			GameplayControl.instance.hpLoseEffect.transform.position = transform.position;
			GameplayControl.instance.hpLoseEffect.hpLoseValue = _dmg;
			GameplayControl.instance.hpLoseEffect.t.text = "Miss";
			yield break;
		}
		GameplayControl.instance.bunny.Damage ();
		GameplayControl.instance.wolf.Damage ();
		GameplayControl.instance.cat.Damage ();

		GameplayControl.instance.hpLoseEffect.transform.position = new Vector3 (GameplayControl.instance.bunny.transform.position.x + 0.3F, GameplayControl.instance.bunny.transform.position.y + 0.3F, 0);
		GameplayControl.instance.hpLoseEffect.hpLoseValue = _dmg;
		Debug.Log ("Take Damage " + _dmg);
		GameplayControl.instance.hp -= _dmg;
	}

	IEnumerator TakeDmgMonster (int _dmg)
	{
		// data la 2 giay
		yield return new WaitForSecondsRealtime (1.4F);
		GameplayControl.instance.monster.characterControl.Damage ();
		GameplayControl.instance.hpLoseEffect.transform.position = GameplayControl.instance.monsterPos.transform.position;
		GameplayControl.instance.hpLoseEffect.hpLoseValue = _dmg;
		Debug.Log ("Take Damage " + _dmg);
		GameplayControl.instance.monsterHp -= _dmg;
		if (GameplayControl.instance.monsterHp > 0) {
			yield return new WaitForSecondsRealtime (0.5F);
			GameplayControl.instance.turnCount--;
			Debug.Log ("Next turn");
		}
	}

	public void Damage ()
	{
		animator.Play ("damage");
	}

	void Animator_AnimationFinished (string obj)
	{
		if (monster.hp > 0) {
			animator.Play ("idle");
		}
	}
}

[System.Serializable]
public enum CharacterState
{
	IDLE,
	ATTACK,
	DAMAGE,
	DEFEATED


}